package com.aplikata.evok.ui;

import java.io.Serializable;
import java.util.Date;
import java.util.List;

import javax.faces.bean.ManagedBean;
import javax.faces.bean.ManagedProperty;
import javax.faces.bean.ViewScoped;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;

import com.aplikata.bo.UserEvent;
import com.aplikata.constants.ConstantsMsg;
import com.aplikata.evok.bo.DoctorSchedule;
import com.aplikata.evok.constants.EvokConstants;
import com.aplikata.evok.service.EvokDoctorService;
import com.aplikata.util.FacesUtil;
import com.aplikata.util.StringUtil;
import com.aplikata.util.YunDateUtil;

/**
 * @ClassName: MReservationBean
 * @Description: TODO
 * @author Huyun
 * @date Sep 18, 2012 11:49:34 AM
 */
@ManagedBean(name = "mReservationBean")
@ViewScoped
public class MReservationBean implements Serializable {

	private static final long serialVersionUID = 1L;
	private Log log = LogFactory.getLog(MReservationBean.class);

	@ManagedProperty(value = "#{evokDoctorService}")
	private EvokDoctorService doctorService;
	private String qyDate;
	
	private UserEvent userEvent;
	private Long eventId;
	private int eventType;
	private String description;
	private DoctorSchedule doctorSchedule;
	private List<Object[]> resList;
	
	private String inputDateFrom;
	private String inputTimeFrom;
	private String inputDateTo;
	private String inputTimeTo;

	public MReservationBean() {
	}

	public void queryAction() {
		try {
			resList = doctorService.getDoctorResForMobile(FacesUtil.getUserFromSession().getId(),
					YunDateUtil.parseDate(qyDate, "dd/MM/yyyy"));
		} catch (Exception e) {
			FacesUtil.addActionListenerException(e, log);
		}
	}
	
	public void todayAction(){
		qyDate = YunDateUtil.formatDate(YunDateUtil.getDate(), "dd/MM/yyyy");
		queryAction();
	}
	
	public String addPrivateAction(){
		eventType = EvokConstants.USER_EVENT_PRIVATE;
		userEvent = new UserEvent(FacesUtil.getUserFromSession());
		userEvent.setTimeFrom(YunDateUtil.getDateTime());
		userEvent.setTimeTo(YunDateUtil.getDateTime());
		description = "";
		
		inputDateFrom = YunDateUtil.formatDate(YunDateUtil.now(), "dd/MM/yyyy");
		inputTimeFrom = YunDateUtil.formatDate(YunDateUtil.now(), "hh:mm a");
		inputDateTo = YunDateUtil.formatDate(YunDateUtil.now(), "dd/MM/yyyy");
		inputTimeTo = YunDateUtil.formatDate(YunDateUtil.now(), "hh:mm a");
		
		return EvokConstants.MOBILE_VIEW_DOCTOR_PRIVATE;
	}
	
	public String submitAction(){
		try {
			if(!YunDateUtil.isValidDate("dd/MM/yyyy", inputDateFrom)){
				FacesUtil.addErrorMessage(ConstantsMsg.MSG_DATE, new Object[]{inputDateFrom, "30/01/2000",
						FacesUtil.getMessage("default_from")});
				return EvokConstants.MOBILE_VIEW_DOCTOR_PATIENT;
			}
			
			if(!YunDateUtil.isValidDate("hh:mm a", inputTimeFrom)){
				FacesUtil.addErrorMessage(ConstantsMsg.MSG_TIME, new Object[]{inputTimeFrom, "02:05 PM",
						FacesUtil.getMessage("default_from")});
				return EvokConstants.MOBILE_VIEW_DOCTOR_PATIENT;
			}
			
			if(!YunDateUtil.isValidDate("dd/MM/yyyy", inputDateTo)){
				FacesUtil.addErrorMessage(ConstantsMsg.MSG_DATE, new Object[]{inputDateTo, "30/01/2000",
						FacesUtil.getMessage("default_to")});
				return EvokConstants.MOBILE_VIEW_DOCTOR_PATIENT;
			}
			
			if(!YunDateUtil.isValidDate("hh:mm a", inputTimeTo)){
				FacesUtil.addErrorMessage(ConstantsMsg.MSG_TIME, new Object[]{inputTimeTo, "02:05 PM",
						FacesUtil.getMessage("default_to")});
				return EvokConstants.MOBILE_VIEW_DOCTOR_PATIENT;
			}
			
			Date dateFrom = YunDateUtil.parseDateTime(inputDateFrom + " " + inputTimeFrom, "dd/MM/yyyy hh:mm a");
			Date dateTo = YunDateUtil.parseDateTime(inputDateTo + " " + inputTimeTo, "dd/MM/yyyy hh:mm a");
			
			if (EvokConstants.USER_EVENT_PRIVATE == eventType) {
				if(StringUtil.isStrEmpty(userEvent.getTitle())){
					FacesUtil.addErrorMessage(ConstantsMsg.MSG_REQUIRE, new Object[]{FacesUtil.getMessage("default_remark")});
					return EvokConstants.MOBILE_VIEW_DOCTOR_PRIVATE;
				}
				userEvent.setTimeFrom(dateFrom);
				userEvent.setTimeTo(dateTo);
				doctorService.merge(userEvent);
			} else if (EvokConstants.USER_EVENT_THERAPY == eventType) {
				if(StringUtil.isStrEmpty(doctorSchedule.getContent())){
					FacesUtil.addErrorMessage(ConstantsMsg.MSG_REQUIRE, new Object[]{FacesUtil.getMessage("default_remark")});
					return EvokConstants.MOBILE_VIEW_DOCTOR_PATIENT;
				}
				
				doctorSchedule.setTimeFrom(dateFrom);
				doctorSchedule.setTimeTo(dateTo);
				doctorService.merge(doctorSchedule);
			}
			queryAction();
			FacesUtil.addInfoMessage(ConstantsMsg.INFO_SUCCESS, new Object[]{""});
			return EvokConstants.MOBILE_VIEW_DOCTOR_RESERVATION;
		} catch (Exception e) {
			FacesUtil.addActionException(e, log);
			return null;
		}
	}

	public String editReservationAction() {
		if (EvokConstants.USER_EVENT_PRIVATE == eventType) {
			userEvent = (UserEvent) doctorService.get(UserEvent.class, eventId);
			inputDateFrom = YunDateUtil.formatDate(userEvent.getTimeFrom(), "dd/MM/yyyy");
			inputTimeFrom = YunDateUtil.formatDateTime(userEvent.getTimeFrom(), "hh:mm a");
			inputDateTo = YunDateUtil.formatDate(userEvent.getTimeTo(), "dd/MM/yyyy");
			inputTimeTo = YunDateUtil.formatDateTime(userEvent.getTimeTo(), "hh:mm a");
			return EvokConstants.MOBILE_VIEW_DOCTOR_PRIVATE;
		} else if(EvokConstants.USER_EVENT_THERAPY == eventType){
			doctorSchedule = (DoctorSchedule) doctorService.get(DoctorSchedule.class, eventId);
			inputDateFrom = YunDateUtil.formatDate(doctorSchedule.getTimeFrom(), "dd/MM/yyyy");
			inputTimeFrom = YunDateUtil.formatDateTime(doctorSchedule.getTimeFrom(), "hh:mm a");
			inputDateTo = YunDateUtil.formatDate(doctorSchedule.getTimeTo(), "dd/MM/yyyy");
			inputTimeTo = YunDateUtil.formatDateTime(doctorSchedule.getTimeTo(), "hh:mm a");
			return EvokConstants.MOBILE_VIEW_DOCTOR_PATIENT;
		}
		return EvokConstants.MOBILE_VIEW_DOCTOR_RESERVATION;
	}
	
	public String deletePrivateAction(){
		try {
			doctorService.delete(userEvent);
			queryAction();
			FacesUtil.addInfoMessage(ConstantsMsg.INFO_SUCCESS, new Object[]{""});
			return EvokConstants.MOBILE_VIEW_DOCTOR_RESERVATION;
		} catch (Exception e) {
			FacesUtil.addActionException(e, log);
			return EvokConstants.MOBILE_VIEW_DOCTOR_PRIVATE_DELETE;
		}
	}
	
	public String deletePatientAction(){
		try {
			doctorService.delete(doctorSchedule);
			queryAction();
			FacesUtil.addInfoMessage(ConstantsMsg.INFO_SUCCESS, new Object[]{""});
			return EvokConstants.MOBILE_VIEW_DOCTOR_RESERVATION;
		} catch (Exception e) {
			FacesUtil.addActionException(e, log);
			return EvokConstants.MOBILE_VIEW_DOCTOR_PRIVATE_DELETE;
		}
	}
	
	public List<Object[]> getResList() {
		if (resList == null) {
			if (FacesUtil.getUserFromSession() != null)
				resList = doctorService.getDoctorResForMobile(FacesUtil.getUserFromSession().getId(), new Date());
		}
		return resList;
	}

	public void setResList(List<Object[]> resList) {
		this.resList = resList;
	}

	public EvokDoctorService getDoctorService() {
		return doctorService;
	}

	public void setDoctorService(EvokDoctorService doctorService) {
		this.doctorService = doctorService;
	}

	public String getQyDate() {
		if (StringUtil.isStrEmpty(qyDate))
			qyDate = YunDateUtil.formatDate(YunDateUtil.getDate(), "dd/MM/yyyy");

		return qyDate;
	}

	public void setQyDate(String qyDate) {
		this.qyDate = qyDate;
	}

	public UserEvent getUserEvent() {
		return userEvent;
	}

	public void setUserEvent(UserEvent userEvent) {
		this.userEvent = userEvent;
	}

	public DoctorSchedule getDoctorSchedule() {
		return doctorSchedule;
	}

	public void setDoctorSchedule(DoctorSchedule doctorSchedule) {
		this.doctorSchedule = doctorSchedule;
	}

	public Long getEventId() {
		return eventId;
	}

	public void setEventId(Long eventId) {
		this.eventId = eventId;
	}

	public int getEventType() {
		return eventType;
	}

	public void setEventType(int eventType) {
		this.eventType = eventType;
	}

	public String getInputDateFrom() {
		return inputDateFrom;
	}

	public void setInputDateFrom(String inputDateFrom) {
		this.inputDateFrom = inputDateFrom;
	}

	public String getInputTimeFrom() {
		return inputTimeFrom;
	}

	public void setInputTimeFrom(String inputTimeFrom) {
		this.inputTimeFrom = inputTimeFrom;
	}

	public String getInputDateTo() {
		return inputDateTo;
	}

	public void setInputDateTo(String inputDateTo) {
		this.inputDateTo = inputDateTo;
	}

	public String getInputTimeTo() {
		return inputTimeTo;
	}

	public void setInputTimeTo(String inputTimeTo) {
		this.inputTimeTo = inputTimeTo;
	}

	public String getDescription() {
		return description;
	}

	public void setDescription(String description) {
		this.description = description;
	}
}
